<?php
class E_IDGeneratorByDb
{
	const QUEUE_SIZE = 10;
	const END_OF_QUEUE = self::QUEUE_SIZE;
	private $offset;
	private $queue;

	public function __construct()
	{	
		$this->queue = array_fill(0, self::QUEUE_SIZE, 0);
		$this->offset = self::END_OF_QUEUE;
	}
	public function getNextID()
	{
		if ($this->offset == self::END_OF_QUEUE)
		{
			$this->fillQueueFromDb();
			$this->offset = 0;
		}
		return $this->queue[$this->offset++];
	}
	
	private function fillQueueFromDb()
	{
		$DB = E_DBConnectionPool::getDbHandle(1);	
        $queueSize = self::QUEUE_SIZE;
		$DB->update("update idgenerator set nextid=LAST_INSERT_ID(nextid+$queueSize)");
		$rowset = $DB->fetch("select LAST_INSERT_ID() as nextid");
		$nextId = $rowset["nextid"];
    	$i = self::END_OF_QUEUE;
    	while ($i > 0)
    	{
    		$this->queue[--$i] = --$nextId;
    	}
	}
}
?>